<template>
    <div>
        <Modal :value="resourceStatus" @on-cancel="cancel" @on-ok="saveResource" :title="'分配权限('+roleName+')'">
            <Tree  :data="baseData" @on-select-change="selectParent" @on-check-change="checkChild" show-checkbox></Tree>
        </Modal>
    </div>
</template>
<script>
    import API from '../../config/api'
    import qs from 'qs'

    export default {
        props: ['resourceStatus', 'roleId','roleName' , 'resourceType'],
        data() {
            return {
                loading: false,
                baseData: [],
                postData:''
            }
        },
        methods: {
            getRescourceList(roleId) {
                this.baseData = [];
                let vm = this;
                this.axios.get(API.GET_RESOURCE_QUERY + roleId + '?resourceType='+this.resourceType).then(response => {
                    if (response.data.code === 1) {
                        /*response.data.data.map((item) => {
                            let obj = {};
                            obj.title = item.resourceName;
                            obj.resourceId = item.resourceId;
                            obj.expand = item.expand;
                            obj.children = [];
                            if(item.childs){
                                item.childs.map((child) => {
                                    let childItem = {};
                                    childItem.title = child.resourceName;
                                    childItem.resourceId = child.resourceId;
                                    childItem.parentId = item.resourceId;
                                    childItem.checked = child.checked;
                                    obj.children.push(childItem)
                                })
                            }
                            vm.baseData.push(obj);
                        })*/
                        vm.baseData = this.buildData(response.data.data);
                    } else {
                        this.$Message.error(response.data.msg);
                    }
                }, response => {
                })
            },
            buildData(data) {
                if (data) {
                    data.forEach(node => {
                        node.expand = node.expand;
                        node.title = node.resourceName;
                        node.children = this.buildData(node.childs);
                    });
                    return data;
                } else {
                    return [];
                }
            },
            saveResource(){
                if(!this.postData){
                    this.$emit('closeModal');
                    return
                }
                let data = {
                    roleId:this.roleId,
                    resource_authorityTypes:this.postData
                };
                this.axios.post(API.SAVE_RESOURCE_ROLE,qs.stringify(data)).then(response => {
                    this.$emit('closeModal');
                    if (response.data.code === 1) {
                        this.$Message.success(response.data.msg);
                    } else {
                        this.$Message.error(response.data.msg);
                    }
                }, response => {
                })
            },
            selectParent(list){
                //console.log(list)
            },
            checkChild(listArray){
                let str = '';
                listArray.map((item)=>{
                    str += item.resourceId + '_1,';
                    if((str.indexOf(item.parentId) == -1) && item.parentId){
                        str += item.parentId + '_1,';
                    }
                })
                this.postData = str.substr(0,str.length-1);
                //console.log(this.postData)
            },
            cancel(){
                this.$emit('closeModal');
            }
        }
    }
</script>
<style lang='scss'>

</style>
